#include <boost/asio/ssl/stream_base.hpp>
#include <cstdio>
#include <iostream>  
#include <boost/asio.hpp>  
#include <boost/system/error_code.hpp>  
#include <boost/asio/ssl.hpp>

class HttpParser
{

};
  
int main() 
{
    // 创建I/O上下文。  
    boost::asio::io_context io_context;  
  
    // 创建SSL上下文并加载CA证书。  
    boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);  
    ctx.load_verify_file("/home/hjie/baidu.com");  
  
    // 创建SSL套接字。  
    boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket(io_context, ctx);  
  
    // 连接到Baidu.com的HTTPS端口（443）。  
    boost::asio::ip::tcp::resolver resolver(io_context);  
    auto endpoints = resolver.resolve("www.baidu.com", "443");  
    boost::system::error_code ec;  
    socket.lowest_layer().connect(*endpoints, ec);  
  
    if (!ec) 
    {
        socket.handshake(boost::asio::ssl::stream_base::client, ec);
        if (ec)
        {
            printf("has error");
        }
        // 发送HTTP请求。  
        std::string request = "GET / HTTP/1.1\r\n";  
        request += "Host: www.baidu.com\r\n";  
        request += "Connection: close\r\n\r\n";  
        socket.write_some(boost::asio::buffer(request), ec);  
  
        if (!ec) 
        {
            // 读取响应。  
            std::array<char, 128> response;  
            for (int index = 0; index < 100; index++)
            {
                size_t response_length = socket.read_some(boost::asio::buffer(response), ec);  
                if (!ec) 
                {  
                    std::cout << std::string(response.data(), response_length); 
                } 
                else 
                {  
                    std::cout << "\nError reading response: " << ec.message() << std::endl;
                    break;
                }  
            }
        } 
        else 
        {  
            std::cout << "Error sending request: " << ec.message() << std::endl;  
        }  
    } 
    else 
    {  
        std::cout << "Error connecting: " << ec.message() << std::endl;  
    }  
    io_context.run();
    // 关闭套接字。  
    return 0;  
}